using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Camalon.ColorPicker;
using Camalon.Drawing;


namespace Camalon.Drawing.Design
{
	/// <summary>
	/// Summary description for TransparenColorEditor.
	/// </summary>
	public class TransparentColorEditor: UITypeEditor
	{

        private static Color[] _myColors;
        private static ListViewStyle _listViewStyle;

        static TransparentColorEditor()
        {
            _myColors = new Color[] { };
            _listViewStyle = ListViewStyle.Details;

        }

		public override bool GetPaintValueSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.Modal;
		}

		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
            if ((value is TransparentColor) || (value is Color) || (value is CompoundColor))
			{

                try
                {
                    Color cl;

                    if (value is TransparentColor) cl = ((TransparentColor)value).Color;
                    else if (value is CompoundColor) cl = ((CompoundColor)value).Color;
                    else cl = (Color)value;

                    Camalon.ColorPicker.ColorDialog colorPicker = new Camalon.ColorPicker.ColorDialog();
                    colorPicker.FullOpen = false;
                    colorPicker.FileOpenOnly = true;
                    colorPicker.AutoName = true;
                    colorPicker.ViewStyle = _listViewStyle;


                    Color[] colors = _myColors;// colorPicker.GetMyColors(true);

                    bool bFound = false;
                    foreach (Color c in colors)
                    {
                        if (c.A == cl.A && c.R == cl.R && c.G == cl.G && c.B == cl.B)
                        { bFound = true; break; }
                    }

                    if (!bFound)
                    {
                        Color[] colorsx = new Color[colors.Length + 1];
                        colors.CopyTo(colorsx, 0);
                        colorsx[colors.Length] = cl;
                        colors = colorsx;
                    }

                    if (colors.Length > 0) colorPicker.SetMyColors(colors, -1);
                    colorPicker.PrimaryColor = cl;

                    if (colorPicker.ShowDialog() == DialogResult.OK) cl = colorPicker.PrimaryColor;

                    _myColors = colorPicker.GetMyColors(true);
                    _listViewStyle = colorPicker.ViewStyle;


                    colorPicker.Dispose();
                    colorPicker = null;


                    if (value is TransparentColor)
                    {
                        TransparentColor c = (TransparentColor)value;
                        c.Color = cl;
                        return c;
                    }
                    else if (value is CompoundColor)
                    {
                        CompoundColor c = (CompoundColor)value;
                        c.Color = cl;
                        return c;
                    }
                    else return cl;
                }
				catch( Exception exc)
				{
                    FileWatcher.WriteLine("TransparentColorEditor::Exception occured" + exc.StackTrace);
				}
			}
		
			return base.EditValue (context, provider, value);
		}



		protected internal static void PaintColorValue( PaintValueEventArgs pe )
		{
            
            if (pe.Value == null) return;

            bool paintable = (pe.Value is TransparentColor) || (pe.Value is CompoundColor) || (pe.Value is Color);

            if (!paintable) return;
		
			Color tc;


			if( pe.Value is TransparentColor)	tc = ((TransparentColor)pe.Value).Color;
			else if( pe.Value is CompoundColor) tc = ((CompoundColor)pe.Value).Color;
			else if( pe.Value is Color )		tc = (Color)pe.Value;
			else tc = (Color)pe.Value;

			bool isEmpty = tc == TransparentColor.Empty;


			Rectangle b = pe.Bounds;
			b.Width--;b.Height--;

			Graphics g = pe.Graphics;

			if( isEmpty )
			{
				Point leftTop		= new Point(b.Left, b.Top);
				Point leftBottom	= new Point(b.Left, b.Bottom);
				Point rightTop		= new Point(b.Right, b.Top);
				Point rightBottom	= new Point(b.Right, b.Bottom);
				Pen pen = Pens.Red;
				g.DrawLine( pen,leftTop, rightBottom);
				g.DrawLine( pen,leftBottom, rightTop);
				return;
			}


			if( tc.A < 255)
			{
				RectangleF rt = b;
				rt.Width /=2f;
				rt.Height/=2f;
				g.FillRectangle( Brushes.Black, rt);
				rt.X += rt.Width;
				rt.Y += rt.Height;
				g.FillRectangle( Brushes.Black, rt);
			}

			using ( SolidBrush sb = new SolidBrush( tc)) g.FillRectangle( sb, b);
				

		}

		public override void PaintValue(PaintValueEventArgs pe) 
		{
				PaintColorValue( pe );

		}
	}
}
